﻿using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Xml;
using System.Text;

namespace imeem.api.json
{
    public class JsonApi : IApiProvider
    {
        private const string API_KEY = "";
        private const string SECRET = "";
        private const string VERSION = "1.0";

        private DataContractJsonSerializer _serializer;

        public string RequestUrl
        {
            get { return "http://www.api.imeem.com/api/json/"; }
        }

        private DataContractJsonSerializer Serializer
        {
            get 
            {
                if (_serializer == null)
                {
                    _serializer = new DataContractJsonSerializer(typeof(imeemResult));
                }
                return _serializer;
            }
        }
        
        public imeemResult Deserialize(string jsonString)
        {
            MemoryStream memStream = null;
            imeemResult result = null;
            try
            {
                memStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
                result = Serializer.ReadObject(memStream) as imeemResult;
            }
            finally
            {
                memStream.Close();
            }
            return result;
        }

        public imeemResult Deserialize(Stream stream)
        {
            return Serializer.ReadObject(stream) as imeemResult;
        }

        public string ApiKey
        {
            get { return API_KEY; }
        }

        public string Secret
        {
            get { return SECRET; }
        }

        public string Version
        {
            get { return VERSION; }
        }
    }
}
